Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Imports SISSAT.DataAcessLayer.StatementSources

Namespace PersistentStorage
    Public Class DbHelper

        Private Shared CONNECTION_STRING As String = DbConnections.GradingSystemDb.ToString()

        Public Shared Function ExecuteNonQuery(ByVal statementSource As IStatementSource) As Integer
            Dim connectionString As String = _
                ConfigurationManager.ConnectionStrings(CONNECTION_STRING).ConnectionString
            Dim command As SqlCommand = statementSource.Command

            Dim value As Integer = 0
            Using connection As New SqlConnection(connectionString)
                connection.Open()
                command.Connection = connection

                value = command.ExecuteNonQuery()
            End Using

            Return value
        End Function

        Public Shared Function ExecuteReader(ByVal statementSource As IStatementSource) As SqlDataReader
            Dim connectionString As String = _
                ConfigurationManager.ConnectionStrings(CONNECTION_STRING).ConnectionString
            Dim command As SqlCommand = statementSource.Command

            Dim connection As New SqlConnection(connectionString)
            connection.Open()
            command.Connection = connection

            Return command.ExecuteReader(CommandBehavior.CloseConnection)
        End Function

    End Class
End Namespace

